//
//  PopoverAnimator.swift
//  TTWeibo
//
//  Created by 唐磊guang on 16/7/5.
//  Copyright © 2016年 唐磊guang. All rights reserved.
//

import UIKit

protocol PopoverAnimatorProtocol: NSObjectProtocol {
    func presentVCPresent()
    func presentVCDissmiss()
}

class PopoverAnimator:NSObject,UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning{
    
    weak var delegate: PopoverAnimatorProtocol?
    
    var isPresent:Bool = false {
        didSet{
            if isPresent {
                delegate?.presentVCPresent()
            }else{
                delegate?.presentVCDissmiss()
            }
        }
    }
    
    var defineFrame:CGRect?
    
    
    //这里写动画时长
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval{
        return 0.5
    }
    //这里写如何动画
    func animateTransition(transitionContext: UIViewControllerContextTransitioning){
        
        if isPresent {
            let toView = transitionContext.viewForKey(UITransitionContextToViewKey)
            toView?.transform = CGAffineTransformMakeScale(1, 0)
            transitionContext.containerView()?.addSubview(toView!)
            
            toView?.layer.anchorPoint = CGPointMake(0.5, 0)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                toView?.transform = CGAffineTransformIdentity
                }, completion: { (_) in
                    transitionContext.completeTransition(true)
            })
        }else{
            let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)
            
            fromView?.layer.anchorPoint = CGPointMake(0.5, 0)
            
            UIView.animateWithDuration(transitionDuration(transitionContext), animations: {
                fromView?.transform = CGAffineTransformMakeScale(1.0, 0.000001)
                }, completion: { (_) in
                    transitionContext.completeTransition(true)
            })
        }
    }
    
    //Mark -- UIViewControllerTransitioningDelegate
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController?
    {
        let popoverVC  = PopoverPresentationController(presentedViewController: presented, presentingViewController: presenting)
        if let frame = defineFrame {
            popoverVC.myDifineFrame = frame
        }
        return popoverVC
    }
    //即将dismissVC时调用
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
        isPresent = false
        return self
    }
    
    //即将展现presentedVC时调用
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning?
    {
         isPresent = true
        return self
    }
}
